package com.tz.actions;

import com.opensymphony.xwork2.ActionSupport;
import com.tz.entity.Cart;
import com.tz.entity.Commodity;
import com.tz.entity.Product;
import com.tz.entity.UserCart;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/24.
 */
@Namespace(value = "/cart")
@ParentPackage("json-default")
public class CartAction extends ActionSupport{

    private UserCart usercart;
    private Commodity commodity;

    @Action(value = "addtocart",results = {
            @Result(type = "json",params = {"root","usercart"})
            })
    public String addtocart(){
        usercart = new UserCart();
        List<Commodity> commodities = new ArrayList<Commodity>();
        for (int i = 0; i < 5; i++) {
            Commodity comm = new Commodity();
            commodities.add(comm);
        }
        Cart cart = new Cart();
        cart.setCommodities(commodities);
        cart.setId(1);
        usercart.setCart(cart);
        usercart.setTotalprice(100);
        return SUCCESS;
    }

    @Action(value = "summary",results = {
            @Result(location = "/product_summary.jsp")
    })
    public String summary(){
        usercart = new UserCart();
        List<Commodity> commodities = new ArrayList<Commodity>();
        double totalprice =0;
        double totaldiscount =0;
        double totaltax =0;
        for (int i = 1; i < 5; i++) {
            Commodity comm = new Commodity();
            Product product = new Product();
            comm.setAmount(i);
            product.setDescription("bala"+i);
            product.setPrice(100+i*10);
            totalprice += product.getPrice()*comm.getAmount();
            product.setDiscount(i*5);
            totaldiscount += product.getDiscount()*comm.getAmount();
            product.setTax(i*3);
            totaltax += product.getTax()*comm.getAmount();
            comm.setProduct(product);
            //comm.setTotal(product.getPrice()-product.getDiscount()+product.getTax());
            commodities.add(comm);
        }
        Cart cart = new Cart();
        cart.setCommodities(commodities);
        cart.setId(1);
        usercart.setCart(cart);
        usercart.setTotalprice(totalprice);
        usercart.setTotaldiscount(totaldiscount);
        usercart.setTotaltax(totaltax);
        usercart.setTotal(totalprice-totaldiscount+totaltax);
        return SUCCESS;
    }

    @Action(value = "editcart",results = {
            @Result(type = "json",params = {"root","usercart"})
    })
    public String editcart(){
        usercart = new UserCart();
        List<Commodity> commodities = new ArrayList<Commodity>();
        double totalprice =0;
        double totaldiscount =0;
        double totaltax =0;
        for (int i = 1; i < 5; i++) {
            Commodity comm = new Commodity();
            Product product = new Product();
            comm.setAmount(i);
            product.setDescription("bala"+i);
            product.setPrice(100+i*10);
            totalprice += product.getPrice()*comm.getAmount();
            product.setDiscount(i*5);
            totaldiscount += product.getDiscount()*comm.getAmount();
            product.setTax(i*3);
            totaltax += product.getTax()*comm.getAmount();
            comm.setProduct(product);
           // comm.setTotal(product.getPrice()-product.getDiscount()+product.getTax());
            commodities.add(comm);
        }
        Cart cart = new Cart();
        cart.setCommodities(commodities);
        cart.setId(1);
        usercart.setCart(cart);
        usercart.setTotalprice(totalprice);
        usercart.setTotaldiscount(totaldiscount);
        usercart.setTotaltax(totaltax);
        usercart.setTotal(totalprice-totaldiscount+totaltax);
        return SUCCESS;
    }
    public UserCart getUsercart() {
        return usercart;
    }

    public void setUsercart(UserCart usercart) {
        this.usercart = usercart;
    }

    public Commodity getCommodity() {
        return commodity;
    }

    public void setCommodity(Commodity commodity) {
        this.commodity = commodity;
    }
}
